
package action;

import db.User;
import java.util.Date;
import java.util.Random;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.DontValidate;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.validation.DateTypeConverter;
import net.sourceforge.stripes.validation.Validate;

/**
 * Register new user.
 * @author Tomas CASTA
 */
@UrlBinding("/reg.action")
public class RegistrationActionBean implements ActionBean {
    /** max number for generating */
    public static final int MAX_RANDOM = 10;
    /** action context */
    private ActionBeanContext context;
    /** new user login, muset be between 2 and 20 chars */
    @Validate(required = true, minlength = 2, maxlength = 20)
    private String login;
    /** new user password, must be longer than 6 chars */
    @Validate(required = true, minlength = 6)
    private String password;
    /** new user password for control, muset be same as password */
    @Validate(required = true, expression = "password == password2")
    private String password2;
    /** new user email, must be valid email*/
    @Validate(required = true, mask = "^[a-zA-Z0-9,!#\\$%&'\\*\\+/=\\?\\^_`\\{\\|}~-]+(\\.[a-zA-Z0-9,!#\\$%&'\\*\\+/=\\?\\^_`\\{\\|}~-]+)*@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.([a-z]{2,})$")
    private String email;
    /** new user info */
    @Validate(required = true)
    private String uinfo;
    /** new user gender */
    @Validate(required = true)
    private String gender;
    /** new user date of birth, must be date and earlier than today */
    @Validate(converter = DateTypeConverter.class, expression = "${this < today}")
    private Date dateOfBirth;
    /** new "captcha" adding two numbers */
    @Validate(required = true, expression="this == (firstNumber + secondNumber)", on="add")
    private String check;
   
    private String msg;
    /** first generated number */
    private int firstNumber;
    /** second generated number */
    private int secondNumber;

    public int getFirstNumber() {
        return firstNumber;
    }

    public int getSecondNumber() {
        return secondNumber;
    }

    public void setFirstNumber(int firstNumber) {
        this.firstNumber = firstNumber;
    }

    public void setSecondNumber(int secondNumber) {
        this.secondNumber = secondNumber;
    }



    public String getCheck() {
        return check;
    }

    public void setCheck(String check) {
        this.check = check;
    }

    public Date getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getMsg() {
        return msg;
    }

    public String getEmail() {
        return email;
    }

    public ActionBeanContext getContext() {
        return context;
    }

    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getLogin() {
        return login;
    }

    public String getPassword() {
        return password;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getGender() {
        return gender;
    }

    public String getPassword2() {
        return password2;
    }

    public String getUinfo() {
        return uinfo;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public void setPassword2(String password2) {
        this.password2 = password2;
    }

    public void setUinfo(String uinfo) {
        this.uinfo = uinfo;
    }

    
    /**
     * Generate new random numbers for add.
     */
    public void generateCheck() {
        Random random = new Random();
        firstNumber = random.nextInt(MAX_RANDOM);
        secondNumber = random.nextInt(MAX_RANDOM);
        
    }


    /**
     * Show registration form.
     * @return Resolution
     */
    @DefaultHandler
    @DontValidate
    public Resolution show() {
        generateCheck();
        return new ForwardResolution("jsp/registration.jsp");
    }

    /**
     * Register new user.
     * @return Resolution.
     */
    public Resolution add() {
        /* Set user from from */
        User user = new User(login, password, email, uinfo, gender, dateOfBirth);
        user.setDb(this.context.getServletContext());
        int result2 = user.addUser();

        /* This login name is already used */
        if (result2 == -1) {
            msg = user.getMsg();
            return new ForwardResolution("jsp/registration.jsp");
        }

        /* DB error */
        if (result2 == -3) {
            msg = user.getErrMsg();
            return new ForwardResolution("jsp/registration.jsp");
        }

        /* OK */
        if (result2 == 1) {
            msg = user.getMsg();
            return new ForwardResolution("jsp/login.jsp");
        }

        /* Some error, show registration form. */
        return new ForwardResolution("jsp/registration.jsp");

     
    }
}
